Array.prototype.myFlat = function(dep){
    let res = []
    for(let i =0; i< this.length; i++){
        if(Array.isArray(this[i]) && dep > 0){
            res = res.concat(this[i].flat(dep - 1))
        } else {
            res.push(this[i])
        }
    }
    return res;
}
console.log([1, [2, [3, 3]]].flat(2))
console.log([1, [2, [3, 3]]].myFlat(2))


var quickSort = function(arr) {
    if(!arr.length) return []
    let left = []
    let right = []
    let povit = arr[0]
    for(let i = 1; i< arr.length; i++){
        if(arr[i] > povit){
            right.push(arr[i])
        } else {
            left.push(arr[i])
        }
    }
    return quickSort(left).concat(povit, quickSort(right))
}
console.log(quickSort([ 3, 4, 1, 5, 2 ]))

var quickSort2 = function(arr){
    return quick(arr, 0, arr.length - 1)
}

var quick = function(arr, l, r){
    let index
    if(l < r){
        index = paration(arr, l, r)
        if(l < index -1){
            quick(arr, l, index-1)
        }
        if(index < r){
            quick(arr, index, r)
        }
    }
    return arr
}

var paration = function(arr, left, right){
    let l = left;
    let r = right;
    let datum = arr[Math.floor(Math.random() * (right - left) + left)]

    while(l <= r){
        while(arr[l] < datum) {l++}
        while(datum < arr[r]) {r--}
        if(l <= r){
            [arr[l], arr[r]] = [arr[r], arr[l]]
            l++
            r--
        }
    }
    return l
}
console.log(quickSort2([ 3, 4, 1, 5, 2 ]), '000')

var insetSort = function(arr){
    for(let i = 1; i< arr.length; i++){
        let temp = arr[i]
        let j = i
        while(j > 0 && arr[j-1] > temp){
            arr[j] = arr[j-1]
            j--
        }
        arr[j] = temp
    }
    return arr
}
console.log(insetSort([ 3, 4, 1, 5, 2 ]))